package com.tj.bo.common.conf.util;

import com.fqgj.common.utils.DecimalUtils;

import java.math.BigDecimal;

/**
 * @program: binary-option
 * @description 计算公式
 * @author: binary
 * @create: 2020-06-05 17:04
 **/
public class CalculatorUtils {

    /**
     * 计算盈利公式
     *
     * @param openingPrice 开盘价格
     * @param closingPrice 收盘价格
     * @param amount       下单金额
     * @param trendType    压涨压跌 0：跌 1：涨
     * @param profit       盈利率
     * @return
     */
    public static BigDecimal calculateProfit(BigDecimal openingPrice, BigDecimal closingPrice, BigDecimal amount, Integer trendType, BigDecimal profit) {
        if (trendType == 0) {
            if (openingPrice.compareTo(closingPrice) == 0) {
                return amount;
            } else if (closingPrice.compareTo(openingPrice) < 0) {
                return DecimalUtils.mul(amount, profit).add(amount);
            } else {
                return BigDecimal.ZERO;
            }
        } else {
            if (openingPrice.compareTo(closingPrice) == 0) {
                return amount;
            } else if (closingPrice.compareTo(openingPrice) > 0) {
                return DecimalUtils.mul(amount, profit).add(amount);
            } else {
                return BigDecimal.ZERO;
            }
        }
    }


    //均值
    public static double getAverage(int num, Integer[] array) {
        int sum = 0;
        for (int i = 0; i < num; i++) {
            sum += array[i];
        }
        return (double) sum / num;
    }

    //标准差
    public static double getStandardDeviation(int num, Integer[] array) {
        double sum = 0;
        for (int i = 0; i < num; i++) {
            sum += ((double) array[i] - getAverage(num, array)) * (array[i] - getAverage(num, array));
        }
        return Math.sqrt(sum / num);
    }

    /**
     * 计算离散系数
     *
     * @param num
     * @param array
     * @return
     */
    public static double getDispersionCoefficient(int num, Integer[] array) {
        double standardDeviation = getStandardDeviation(num, array);
        double average = getAverage(num, array);
        double d = standardDeviation / average;
        return d;
    }

    /**
     * 计算大盘收益率
     * 预估胜率 = (上笔交易金额 + 胜率*新赔付金额)/新赔付金额
     * 收益率 = 87.5%(这个值固定不变) / 50.1% -1 = 74%
     *
     * @param compensationAmount
     * @param lastTransactionAmount
     * @param winRate
     * @return
     */
    public static BigDecimal calculateProfitByCompensation(BigDecimal compensationAmount, BigDecimal lastTransactionAmount, BigDecimal winRate) {

        BigDecimal estimateWinRate = (lastTransactionAmount.add(winRate.multiply(compensationAmount))).divide(compensationAmount, 4, BigDecimal.ROUND_HALF_UP);
        BigDecimal profitRate = new BigDecimal(0.875).divide(estimateWinRate, 4, BigDecimal.ROUND_HALF_UP).subtract(new BigDecimal(1)).setScale(2, BigDecimal.ROUND_HALF_UP);
        return profitRate;
    }

    public static void main(String[] args) {
        System.out.println(calculateProfitByCompensation(new BigDecimal(40000), new BigDecimal(57.558354), new BigDecimal(0.5061)));
    }
}
